package view;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
import org.dyno.visual.swing.layouts.Trailing;

import twitter4j.User;
import utils.ListUtils;

import com.algorithms.ScoredUser;
import com.algorithms.TwitterApi;

//VS4E -- DO NOT REMOVE THIS LINE!
public class ResultsFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JButton jButton0;
	private JButton jButton1;
	private JTextArea jTextArea0;
	private JScrollPane jScrollPane0;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";

	public ResultsFrame() {
		initComponents();
	}

	private void initComponents() {
		setLayout(new GroupLayout());
		add(getJButton0(), new Constraints(new Trailing(12, 61, 314),
				new Leading(43, 12, 12)));
		add(getJButton1(), new Constraints(new Trailing(12, 61, 314),
				new Leading(75, 12, 12)));
		add(getJScrollPane0(), new Constraints(new Bilateral(27, 129, 22),
				new Leading(43, 269, 10, 10)));
		setSize(636, 359);
		this.setLocationRelativeTo(null);
	}

	private JScrollPane getJScrollPane0() {
		if (jScrollPane0 == null) {
			jScrollPane0 = new JScrollPane();
			jScrollPane0.setViewportView(getJTextArea0());
		}
		return jScrollPane0;
	}

	private JTextArea getJTextArea0() {
		if (jTextArea0 == null) {
			jTextArea0 = new JTextArea();
			jTextArea0.setText("");
		}
		return jTextArea0;
	}

	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Algoritmo 2");
			jButton1.addMouseListener(new MouseAdapter() {

				public void mouseClicked(MouseEvent event) {
					jButton1MouseMouseClicked(event);
				}
			});
		}
		return jButton1;
	}

	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.setText("Algortimo 1");
			jButton0.addMouseListener(new MouseAdapter() {

				public void mouseClicked(MouseEvent event) {
					jButton0MouseMouseClicked(event);
				}
			});
		}
		return jButton0;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class. Note: This class is only created so that you can
	 * easily preview the result at runtime. It is not expected to be managed by
	 * the designer. You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				ResultsFrame frame = new ResultsFrame();
				frame.setDefaultCloseOperation(ResultsFrame.EXIT_ON_CLOSE);
				frame.setTitle("Selección de Algoritmos");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

	private void jButton0MouseMouseClicked(MouseEvent event) {
		this.jTextArea0.setText("");
		this.jButton0.setEnabled(false);
		this.jButton1.setEnabled(false);

		JDialog politics = new PoliticSelector(this);
		politics.setLocationRelativeTo(this);
		politics.setTitle("Opciones");
		politics.setVisible(true);
		this.setEnabled(false);

	}

	private void jButton1MouseMouseClicked(MouseEvent event) {

		this.jTextArea0.setText("");
		this.jButton0.setEnabled(false);
		this.jButton1.setEnabled(false);

		JDialog topTweets = new TopTweetsSelector(this);
		topTweets.setLocationRelativeTo(this);
		topTweets.setTitle("Opciones");
		topTweets.setVisible(true);
		this.setEnabled(false);

	}

	public void showUsers(List<ScoredUser> scoredUsers) {

		List<User> users = TwitterApi.getUsersById(scoredUsers);

		StringBuffer buff = new StringBuffer();

		int i = 1;
		for (User user : users) {
			buff.append("recomendación ").append(i++);
			buff.append(") user name: ").append(user.getName());
			buff.append("\tuser screen name: ").append(user.getScreenName());
			buff.append("\tuser id: ").append(user.getId());
			buff.append("\tscore: ").append(
					ListUtils.getScore(scoredUsers, user.getId()));
			buff.append(".\n");
		}

		if (buff.toString().trim().equals("")) {
			buff.append("no hay resultados");
		}
		this.getJTextArea0().setText(buff.toString());

		this.jButton0.setEnabled(true);
		this.jButton1.setEnabled(true);
	}
}